<template>
  <div>
    <ul class="movie-list" v-if="dataList">
      <li
        class="movie-item"
        v-for="item in dataList"
        :key="item.filmId"
        @click="toDetial(item.filmId)"
      >
        <img class="poster" :src="item.poster" alt="" />
        <div class="info">
          <h3 class="name">{{ item.name }}</h3>
          <p>观众评分：{{ item.grade }}</p>
          <p class="actors">主演：{{ item.actors | actorsFilter }}</p>
          <p>{{ item.nation }} | {{ item.runtime }}分钟</p>
        </div>
        <van-button class="buy" plain type="info" size="small">购票</van-button>
      </li>
    </ul>
    <van-loading
      v-show="isLoadingShow"
      type="spinner"
      color="#f00"
      size="36px"
      vertical
    >
      数据加载中...
    </van-loading>
    <p class="bottom-mes" v-show="bottomMes">已到底</p>
  </div>
</template>

<script>
import { getNowPlayingAPI } from "@/network/api";

import Vue from "vue";
import { Loading, Button } from "vant";
Vue.use(Loading);
Vue.use(Button);

export default {
  name: "NowPlaying",
  data() {
    return {
      dataList: [],
      page: 1,
      total: 0,
      isLoadingShow: true,
      bottomMes: false,
    };
  },
  filters: {
    actorsFilter(data) {
      return data && data.map((val) => val.name).join(" ");
    },
  },
  methods: {
    toDetial(id) {
      this.$router.push("/detail/" + id);
    },
    // 下拉数据更新方法（父组件下拉调用此方法）
    scrollEndGetMore() {
      this.page++; // 页码每次+1，axios根据page请求数据
      if (this.dataList.length !== this.total) {
        getNowPlayingAPI({
          cityId: 110100,
          pageNum: this.page,
          pageSize: 10,
          type: 1,
          k: 227100,
        }).then((res) => {
          console.log(res);
          // 把请求到的新数据插入到原来的数组里面
          this.dataList = [...this.dataList, ...res.data.data.films];
          if (this.dataList.length == this.total) {
            this.isLoadingShow = false;
            this.bottomMes = true;
          }
        });
      }
    },
  },
  mounted() {
    getNowPlayingAPI({
      cityId: 110100,
      pageNum: 1,
      pageSize: 10,
      type: 1,
      k: 227100,
    }).then((res) => {
      this.dataList = res.data.data.films;
      this.total = res.data.data.total;
      console.log("NowPlaying默认第一页请求成功", this.dataList);
    });
  },
};
</script>

<style scoped  lang="less" >
.movie-list {
  padding: 10px 0;
}
.movie-item {
  padding: 10px;
  display: flex;
  background-color: #fff;
  margin: 5px 10px;
  border-radius: 10px;
  color: #797d82;
  .poster {
    width: 90px;
    height: 130px;
  }
  .info {
    padding: 10px;
    width: 180px;
    font-size: 16px;
    .name {
      font-weight: 500;
      color: #333;
      font-size: 18px;
      margin-bottom: 5px;
    }
    .actors {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .buy {
    // width: 50px;
    margin-top: 50px;
  }
}
.bottom-mes {
  text-align: center;
  height: 26px;
  line-height: 14px;
  background-color: #eee;
}
</style>